<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Orchid\Platform\Models\Role;
use Orchid\Platform\Models\User;

class AdminRoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 创建管理员角色
        $adminRole = Role::firstOrCreate(
            ['slug' => 'admin'],
            [
                'name' => '管理员',
                'permissions' => [
                    // 系统权限
                    'platform.systems.roles' => true,
                    'platform.systems.users' => true,
                    
                    // 内容权限
                    'platform.content.posts' => true,
                    'platform.content.pages' => true,
                    'platform.content.categories' => true,
                    'platform.content.tags' => true,
                    'platform.media' => true,
                    
                    // 设置权限
                    'platform.settings' => true,
                    'platform.settings.menu' => true,
                    
                    // 基础权限
                    'platform.index' => true,
                ]
            ]
        );

        // 查找admin用户
        $adminUser = User::where('email', 'admin@admin.com')->first();
        
        if ($adminUser) {
            // 为admin用户分配管理员角色
            $adminUser->roles()->syncWithoutDetaching([$adminRole->id]);
            
            // 直接为用户分配所有权限
            $adminUser->permissions = [
                // 系统权限
                'platform.systems.roles' => true,
                'platform.systems.users' => true,
                
                // 内容权限
                'platform.content.posts' => true,
                'platform.content.pages' => true,
                'platform.content.categories' => true,
                'platform.content.tags' => true,
                'platform.media' => true,
                
                // 设置权限
                'platform.settings' => true,
                'platform.settings.menu' => true,
                
                // 基础权限
                'platform.index' => true,
            ];
            $adminUser->save();
            
            $this->command->info('Admin user permissions updated successfully.');
        } else {
            $this->command->error('Admin user not found.');
        }
    }
}